import request from '@/hooks/utils/http'
import { AxiosPromise } from 'axios'
import { FlowTaskVO } from '@/service/FlowTaskService'

export class InstanceService {

    /**
     * 查询运行中实例列表
     * @param query
     * @returns {*}
     */
    static pageByRunning = (query: FlowInstanceQuery): AxiosPromise<FlowInstanceVO[]> => {
        return request({
            url: '/workflow/instance/pageByRunning',
            method: 'get',
            params: query
        })
    }

    /**
     * 查询已完成实例列表
     * @param query
     * @returns {*}
     */
    static pageByFinish = (query: FlowInstanceQuery): AxiosPromise<FlowInstanceVO[]> => {
        return request({
            url: '/workflow/instance/pageByFinish',
            method: 'get',
            params: query
        })
    }

    /**
     * 通过业务id获取历史流程图
     */
    static flowHisTaskList = (businessId: string | number) => {
        return request({
            url: `/workflow/instance/flowHisTaskList/${businessId}` + '?t' + Math.random(),
            method: 'get'
        })
    }

    /**
     * 分页查询当前登录人单据
     * @param query
     * @returns {*}
     */
    static pageByCurrent = (query: FlowInstanceQuery): AxiosPromise<FlowInstanceVO[]> => {
        return request({
            url: '/workflow/instance/pageByCurrent',
            method: 'get',
            params: query
        })
    }

    /**
     * 撤销流程
     * @param data 参数
     * @returns
     */
    static cancelProcessApply = (data: any) => {
        return request({
            url: `/workflow/instance/cancelProcessApply`,
            method: 'put',
            data: data
        })
    }

    /**
     * 获取流程变量
     * @param instanceId 实例id
     * @returns
     */
    static instanceVariable = (instanceId: string | number) => {
        return request({
            url: `/workflow/instance/instanceVariable/${instanceId}`,
            method: 'get'
        })
    }

    /**
     * 删除
     * @param instanceIds 流程实例id
     * @returns
     */
    static deleteByInstanceIds = (instanceIds: Array<string | number> | string | number) => {
        return request({
            url: `/workflow/instance/deleteByInstanceIds/${instanceIds}`,
            method: 'delete'
        })
    }
    /**
     * 作废流程
     * @param data 参数
     * @returns
     */
    static invalid = (data: any) => {
        return request({
            url: `/workflow/instance/invalid`,
            method: 'post',
            data: data
        })
    }

}

export interface FlowInstanceQuery extends PageQuery {
    category?: string | number;
    nodeName?: string;
    flowCode?: string;
    flowName?: string;
    createByIds?: string[] | number[];
    businessId?: string;
}

export interface FlowInstanceVO extends BaseEntity {
    id: string | number;
    definitionId: string;
    flowName: string;
    flowCode: string;
    version: string;
    businessId: string;
    activityStatus: number;
    tenantId: string;
    createTime: string;
    createBy: string;
    flowStatus: string;
    flowStatusName: string;
    flowTaskList: FlowTaskVO[];
}
